package com.abel.main.onmessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class MessageProducer {

    public static void main(String[] args) {
        String message = "哈哈 消息已经发送了";
        sendMessage(message);
    }

    private static void sendMessage(String message) {
        ConnectionFactory factory = null;
        Connection connection = null;
        Session session = null;
        Destination destination = null;
        javax.jms.MessageProducer producer = null;
        Message messageObj = null;

        try {
            factory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.26.50:61616");
            connection = factory.createConnection();
            connection.start();

            session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            destination = session.createQueue("onmessage");
            producer = session.createProducer(destination);
            ObjectMessage obj = session.createObjectMessage(message);
//            messageObj = session.createTextMessage(message);
            producer.send(obj);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (producer != null) {
                try {
                    producer.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
